<template>
  <div class="system-user-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
      <el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="名称">
              <el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="手机号">
              <el-input v-model="state.ruleForm.phone" placeholder="请输入手机号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.dialog.type==='edit'">
            <el-form-item label="未支付订单数" label-width="100px">
              <el-input v-model="state.ruleForm.unpayOrderNum" clearable :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.dialog.type==='edit'">
            <el-form-item label="未支付订单金额" label-width="110px">
              <el-input v-model="state.ruleForm.unpaidOrderAmount" clearable :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="备注">
              <el-input v-model="state.ruleForm.remake" placeholder="请输入备注" clearable
                        type="textarea" :rows="3" :max-rows="5"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="systemUserDialog">
import {reactive, ref} from 'vue';
import {addUser, updateUser} from "/@/api/user/user";

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const state = reactive({
  ruleForm: {
    id: '', // 编号
    name: '', // 名称
    phone: '', // 手机号
    remake: '', // 备注
    unpayOrderNum: '', // 未支付订单数
    unpaidOrderAmount: '', // 未支付订单金额
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
});

// 打开弹窗
const openDialog = async (type: string, row?: any) => {
  state.dialog.isShowDialog = true;
  state.dialog.type = type;
  if (type === 'edit') {
    // 处理编辑场景：转换时间字符串为Date对象
    state.ruleForm = {
      id: row.id,
      name: row.name,
      phone: row.phone,
      unpayOrderNum: row.unpayOrderNum,
      unpaidOrderAmount: row.unpaidOrderAmount,
      remake: row.remake,
    };
    state.dialog.title = '修改用户';
    state.dialog.submitTxt = '修 改';
  } else {
    // 新增场景：重置表单
    state.ruleForm = {
      name: '',
      phone: '',
      remake: '',
    };
    state.dialog.title = '新增用户';
    state.dialog.submitTxt = '新 增';
  }
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交操作
const onSubmit = (type: any) => {
  // 示例：打印最终表单数据（实际项目中替换为接口调用）
  console.log('提交的数据：', state.ruleForm);
  console.log(type);
  if (type === 'add') {
    state.ruleForm = {
      id: state.ruleForm.id,
      name: state.ruleForm.name,
      phone: state.ruleForm.phone,
      remake: state.ruleForm.remake,
    }
    addUser(state.ruleForm)
  } else {
    updateUser(state.ruleForm)
  }
  closeDialog();
  emit('refresh');
};


// 暴露变量
defineExpose({
  openDialog,
});
</script>
